"""
    Topic：bilibili 登录脚本
    Version：2.0
    Date：2021-6-17
    Info：免密登录 By Cookies
    Tip：高驱动版本90.左右，Cookies存在注入失败，这里先打开登录，再打开主页;chropath插件，定位xpath
"""
from selenium import webdriver
import json
import time
# 驱动直接放到当前路径下
driver = webdriver.Chrome()
url = 'https://www.bilibili.com/video/BV16E411H7og?from=search&seid=5409947877497196900/'
# url = 'https://www.bilibili.com/'
login_url = 'https://passport.bilibili.com/login?from_spm_id=333.851.top_bar.login'
driver.get(url=login_url)

#注入cookies
with open('cookies.txt',encoding='utf-8') as file:
    json_cookies_string = file.read()
json_cookies_list = json.loads(json_cookies_string)
# print(json_cookies_list)
for cookie in json_cookies_list:
    driver.add_cookie(cookie)
#刷新
#driver.refresh()
driver.get(url=url)


#测试发评论                ????????
time.sleep(10)
#selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="ipt-txt"]"}
text = driver.find_element_by_name('ipt-txt')
# text = driver.find_element_by_xpath('//*[@id="comment"]/div/div[2]/div/div[3]/div[2]/textarea')
text.send_keys('qiang ya')
btn = driver.find_element_by_xpath('//*[@id="comment"]/div/div[2]/div/div[3]/div[2]/button')
btn.click()